package ute.mvc.controller.portal.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ute.mvc.service.ClassService;
import ute.mvc.service.UserService;
import ute.mvc.util.JsonObject;
import ute.mvc.model.MyClass;
import ute.mvc.model.Course;
import ute.mvc.model.User;

@Controller
@RequestMapping(value = {"/portal/class"})
public class ClassController {
	@Autowired
	ClassService classService;
	@Autowired
	UserService userService;
	private static String classId;
	
	
	@RequestMapping(value = "/{classId}", method = RequestMethod.GET)
	public String getClassInfo(@PathVariable String classId, Model model) {
		
		this.classId = classId;
		
		model.addAttribute("myClass", classService.getClassById(classId));
		
		/*MyClass myClass = classService.getClassByIdWithListStudent(classId);
		myClass.getListStudent().add(userService.getUserByEmail("tienducitt@gmail.com2"));
		classService.updateClass(myClass);
		*/
		return "class_detail";
	}
	
	@RequestMapping(value = "/edit/{classId}", method = RequestMethod.GET)
	public String getEditClass(@PathVariable String classId, Model model) {
		
		model.addAttribute("classId", classId);
		model.addAttribute("myClass", classService.getClassById(classId));
		return "class_edit";
	}
	
	@RequestMapping(value = "/edit/{classId}", method = RequestMethod.POST)
	public String editClass(@PathVariable String classId, @Valid @ModelAttribute MyClass myClass, BindingResult result, Model model) {
		if(result.hasErrors()) {
			return "class_edit";
		}
		MyClass exitClass = classService.getClassById(classId);
		exitClass.setClassName(myClass.getClassName());
		
		classService.updateClass(exitClass);
		return "redirect:../" + classId;
	}
	
	@RequestMapping(value = "/addstudent/{classId}", method = RequestMethod.GET)
	public String getAddStudent(@PathVariable String classId, Model model) {
		this.classId = classId;
		model.addAttribute("classId", classId);
		return "student_add";
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/listStudentJSON")
	@ResponseBody
	public JsonObject getStudentJSON() {
		System.out.println("lay student");
		List<User> list = classService.getListStudentByClassId(classId);
		System.out.println(list.size());
		
		JsonObject Json = new JsonObject();
		Json.setAaData(list);
		Json.setsEcho(1);
		Json.setiTotalDisplayRecords(list.size());
		Json.setiTotalRecords(list.size());
		return Json;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "addstudent/listStudentJSON")
	@ResponseBody
	public JsonObject getAddStudentJSON() {
		System.out.println("lay student" + classId);
		List<User> list = classService.getAddUserById(classId);
		System.out.println(list.size());
		
		JsonObject Json = new JsonObject();
		Json.setAaData(list);
		Json.setsEcho(1);
		Json.setiTotalDisplayRecords(list.size());
		Json.setiTotalRecords(list.size());
		return Json;
	}
	
	@ResponseBody
	@RequestMapping("/addstudent/add")
	public String addStudentToClass(@RequestBody String users) {
		try {
			MyClass myClass;
			myClass = classService.getClassByIdWithListStudent(classId);
			
			String[] ids = users.split(";");
			for(String id : ids){
				myClass.getListStudent().add(userService.getUserById(Integer.parseInt(id)));
			}
			classService.updateClass(myClass);
			return "sucess";
		} catch (Exception ex) {
			return "fail";
		}
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public String removeStudent(@RequestBody String users) {
		String[] emails = users.split(";");
		if(classService.removeStudent(emails, classId)){
			return "success";
		} else {
			return "fail";
		}
	}
}
